package com.cykj.web.controller.posbiz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.BizAfullOperationRecordSheet;
import com.cykj.pos.domain.BizPosMachine;
import com.cykj.pos.domain.dto.WalletNewDTO;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.service.IBizAfullOperationRecordSheetService;
import com.cykj.pos.service.IBizPosMachineService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.domain.BizPosMachineStatusRecords;
import com.cykj.pos.service.IBizPosMachineStatusRecordsService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 终端设备状态记录Controller
 *
 * @author weijianbo
 * @date 2021-02-02
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/posmachine/statusrecords" )
public class BizPosMachineStatusRecordsController extends BaseController {

    private final IBizPosMachineStatusRecordsService iBizPosMachineStatusRecordsService;
    private final IBizPosMachineService posMachineService;
    @Autowired
    BizPosMachineMapper posMachineMapper;
    @Autowired
    private IBizAfullOperationRecordSheetService bizAfullOperationRecordSheetService;
    /**
     * 查询终端设备状态记录列表
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        startPage();
        List<BizPosMachineStatusRecords> list = iBizPosMachineStatusRecordsService.queryList(bizPosMachineStatusRecords);
        return getDataTable(list);

    }
    /**
     * 查询终端设备已激活列表
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:listActiv')")
    @GetMapping("/listActiv")
    public TableDataInfo listActiv(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (sysUser.getUserId() == 1){
            sysUser.setUserId(2L);
        }
        Long merchId = posMachineMapper.selectMerchId(sysUser.getUserId());
        bizPosMachineStatusRecords.setMerchId(merchId);
        startPage();
        List<BizPosMachineStatusRecords> list = iBizPosMachineStatusRecordsService.queryListActiv(bizPosMachineStatusRecords);
        return getDataTable(list);

    }

    /**
     * vue商户转移
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:updateTransfer')")
    @PostMapping("/updateTransfer")
    public AjaxResult updateMerchantsTransfer(BizPosMachine bizPosMachine) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.format(date);
        bizPosMachine.setUpdateTime(date);
        BizPosMachine bizPos=posMachineService.getPosMachineBySnCode(bizPosMachine.getPosCode());
        bizPosMachine.setVar3(String.valueOf(bizPos.getMerchId()));
        int i=posMachineService.updateMerchantsTransferVue(bizPosMachine);
        return toAjax(i);
    }


    /**
     * 导出终端设备状态记录列表
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:export')" )
    @Log(title = "终端设备状态记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        List<BizPosMachineStatusRecords> list = iBizPosMachineStatusRecordsService.queryList(bizPosMachineStatusRecords);
        ExcelUtil<BizPosMachineStatusRecords> util = new ExcelUtil<BizPosMachineStatusRecords>(BizPosMachineStatusRecords.class);
        return util.exportExcel(list, "statusrecords" );
    }

    /**
     * 获取终端设备状态记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBizPosMachineStatusRecordsService.getById(id));
    }

    /**
     * 新增终端设备状态记录
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:add')" )
    @Log(title = "终端设备状态记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        return toAjax(iBizPosMachineStatusRecordsService.save(bizPosMachineStatusRecords) ? 1 : 0);
    }

    /**
     * 修改终端设备状态记录
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:edit')" )
    @Log(title = "终端设备状态记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPosMachineStatusRecords bizPosMachineStatusRecords) {
        return toAjax(iBizPosMachineStatusRecordsService.updateById(bizPosMachineStatusRecords) ? 1 : 0);
    }

    /**
     * 删除终端设备状态记录
     */
    @PreAuthorize("@ss.hasPermi('posmachine:statusrecords:remove')" )
    @Log(title = "终端设备状态记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBizPosMachineStatusRecordsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @PostMapping("/selectIsTransfer")
    public AjaxResult selectIsTransfer(WalletNewDTO bizPosMachine) {
        AjaxResult ajaxResult=AjaxResult.success();
        /*查看是否需要补件*/
        int a=posMachineService.selectIsTransfer(bizPosMachine);
        if (a>0){
            ajaxResult.put("code",200);
            ajaxResult.put("msg","可以划拨");
            return ajaxResult;
        }else {
            BizPosMachine bizPosMachine1=posMachineService.selectPosMachineType(bizPosMachine);
            if (Integer.parseInt(bizPosMachine1.getPosType())<=3){
                ajaxResult.put("code",200);
                ajaxResult.put("msg","可以划拨");
                return ajaxResult;
            }else {
                ajaxResult.put("code",500);
                ajaxResult.put("msg","不能划拨，需要先同步报件数据");
                return ajaxResult;
            }
        }
    }

    /*商户转移----只转商户不转钱 转交易*/
    @PostMapping("/updateMerchants/merchant/transaction/not/money")
    public AjaxResult updateOldMerchantsMerchantTransactionNotMoney(WalletNewDTO bizPosMachine) {
        Long merchId=bizPosMachine.getMerchId();
        String posCode=bizPosMachine.getPosCode();
        /*在设备管理中查询   pos类型 选择 merch_id*/
        BizPosMachine bizPosMachine1=posMachineService.selmerchId1(posCode);
        Long merchId1=bizPosMachine1.getMerchId();
        /*积分账单    查询msg_id 获取选中人的user_id*/
        Long userId1=posMachineService.seluserid1(merchId1);
        /*积分账单    查询msg_id 获取指向人的user_id*/
        Long userId=posMachineService.seluserid(merchId);
        int a=posMachineService.updateMerchantsEquipmentVue(merchId,posCode);
        BizAfullOperationRecordSheet bizAfullOperationRecordSheet=new BizAfullOperationRecordSheet();
        bizAfullOperationRecordSheet.setPosCode(posCode);
        bizAfullOperationRecordSheet.setOldApplication(userId1);
        bizAfullOperationRecordSheet.setNewApplication(userId);
        bizAfullOperationRecordSheet.setOldmerchId(merchId1);
        bizAfullOperationRecordSheet.setNewmerchId(merchId);
        //获取当前时间
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        Date newtime= null;
        try {
            newtime = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        bizAfullOperationRecordSheet.setOperateTime(newtime);
        bizAfullOperationRecordSheet.setOperateType("2.4");
        bizAfullOperationRecordSheet.setOperator(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        bizAfullOperationRecordSheetService.insertFullrecords(bizAfullOperationRecordSheet);
        /*查询报件表id*/
        Long id=posMachineService.selectMerchantsEntry(posCode);
        /*修改报件表所属代理*/
        int b =posMachineService.updateMerchantsEntry(merchId,id);
        return toAjax(a);
    }
}
